#!/usr/bin/python
# -*- coding: UTF-8 -*-
from datetime import datetime,timedelta
import os, string, sys, time, re, math, fileinput, glob, shutil

pathCur = sys.path[0] #示例：d:\\soft\\safe

class PathBuilder:
    
    def getRoot(self):
        cur = os.path.abspath(__file__)
        cur = os.path.dirname(cur)
        return os.path.join(cur,"upload")

    # 生成文件存储路径
    # 年/月/日/guid/nameLoc
    def mkFile(self,id,nameLoc):
        now = datetime.now()
        dir = os.path.join(self.getRoot(), str(now.year))
        dir = os.path.join(dir, str(now.month))
        dir = os.path.join(dir, str(now.day))
        dir = os.path.join(dir,id)
        dir = os.path.join(dir,nameLoc)
        return dir
    
    # 绝对路径转相对路径
    def absToRel(self,p):
        p = p.replace(self.getRoot(),"")
        return p
    
    #相对路径转绝对路径
    def relToAbs(self,p):
        root = self.getRoot()

        if p.startswith(root)==True:
            return p
        
        if p[0]=='\\' or p[0]=='/':
            p = p[1:]
        
        p = os.path.join(root,p)
        return p